//
//  HMRemindMatterCell.swift
//  HealthMonitoring
//
//  Created by 珠海正杰 on 2018/8/29.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON


 protocol HMRemindMatterCellDelegate: class {
//
      func matterCell(_ id: Int, itemType: String, view: MatterItemView, cellInt: IndexPath, itemTag: Int, value: Any?, itemJson: JSON)
//      func matterCell(_ id: Int, itemType: String, cellIndexPath: IndexPath, itemTag: Int, value: Any?)
}


class HMRemindMatterCell: UITableViewCell {

    var cellHeight: CGFloat = 0.0
    var dataList: JSON? = nil
    weak var delegate: HMRemindMatterCellDelegate?
    
    var addItems = Array<MatterItemView>()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    class func cell(json:JSON, tableView: UITableView, cellForRowAt indexPath: IndexPath) -> HMRemindMatterCell? {
        
      let identifier = "HMRemindMatterCell"
        var cell: HMRemindMatterCell? = tableView.dequeueReusableCell(withIdentifier: identifier) as? HMRemindMatterCell
        if cell == nil {
            cell = HMRemindMatterCell.init(style: .default, reuseIdentifier: identifier)
        }
        cell?.setupViews(json: json, tableView: tableView, view: (cell?.contentView)!, indexPath: indexPath)
        return cell
    }
    func setupViews(json: JSON, tableView: UITableView, view: UIView, indexPath: IndexPath) {
        
        
        
        if addItems.count > 0 {
            for itemView in addItems {  //全部删除
                itemView.removeFromSuperview()
            }
        }
        
            var recordHeight: CGFloat = 0.0
            let newAry = json["itme"]
            let row = json["indexId"].int
            
            for (v,ary) in newAry {
                if ary["isHides"].boolValue == false {
                    let item = MatterItemView.init(json: ary)
                    item.isUserInteractionEnabled = true
                    item.tag = Int(v)!  //itme 序列号
                    item.indexPath = indexPath  // Cell row
                    item.indexPathRow = indexPath.row
                    view.addSubview(item)
                    addItems.append(item)
                    // 可以控制整个容器的长宽高
                    let topH =  recordHeight //*  CGFloat(Int(v)!)  //CGFloat(44) * CGFloat(Int(v)!) + 10 //* Int(v)!
                    recordHeight += item.matterItemHeight
                    item.topAlign(view: view, predicate: "\(topH)")
                    item.leadingAlign(view: view, predicate: "20")
                    item.trailingAlign(view: view, predicate: "-20")
                    item.heightConstrain(predicate: "\(item.matterItemHeight)")
                    item.matterItemBlock = {
                        (view, type, cellInt, tag, value, itemJson) -> () in

                        self.delegate?.matterCell(row!, itemType: type, view: view, cellInt: indexPath, itemTag: tag, value: value, itemJson: itemJson)
                    }
                }
            }
        
           // print("打印高度", recordHeight)
            cellHeight = recordHeight
    }
    
}
